import axios from '../../utils/http';
import { observable, action } from 'mobx';
import { message } from 'antd';
class AddGoodsMobx {
  /* 添加的表单 */
  @observable addForm = {
    // goodsId: '' /* 商品Id */,
    goodsName: '' /* 商品名称 */,
    goodsCode: '' /* 商品编号 */,
    goodsCid: '' /* 商品分类 */,
    goodsJj: '' /* 商品简介 */,
    pllIds: '' /* 商品标签 */,
    goodsCothotr: '' /* 团长佣金 */,
    goodsLq: '' /* 限购数量 */,
    goodsPrice: '' /* 商品价格 */,
    goodsCd: '' /* 商品详情 */,
    goodsCw: '' /* 商品重量 */,
    goodsC: 'kg' /* 商品重量单位 */,
    goodsCi: '' /* 商品库存 */,
    goodsIw: 1 /* 库存警告 */,
    goodsRs: 1 /* 加入推荐 */,
    goodsFss: 1 /* 免 运 费 */,
    goodsRemarks: '' /* 商家备注*/,
    file1: '' /* 商品图片*/,
    file2: '' /* 拼团图片*/,
    file3: '' /* 商品相册 */,
    comspec: '' /* 商品规格 */,
    gp: '' /* 拼团 */,
    goodsMp: '',
  };
  @observable gps = []; /* 拼团规则数组 */
  @observable coms = []; /* 商品规格数组 */

  /* 普通类型修改 */
  @action formChange = (name, val) => {
    this.addForm[name] = val;
  };

  /* 复杂类型修改 */
  @action formChangePro = (id, type, name, val) => {
    type === 1
      ? (this.gps[this.gps.findIndex(item => item.id === id)].val[name] = val)
      : (this.coms[this.coms.findIndex(item => item.id === id)].val[name] = val);
  };

  /* 添加拼团 */
  @action addGp = gp => {
    this.gps.push(gp);
  };

  /* 删除拼团规则数组*/
  @action delGp = id => {
    this.gps.splice(
      this.gps.findIndex(item => item.id === id),
      1
    );
  };

  /* 添加规格*/
  @action addComspec = coms => {
    this.coms.push(coms);
  };

  /* 删除商品规格数组*/
  @action delComspec = id => {
    this.coms.splice(
      this.coms.findIndex(item => item.id === id),
      1
    );
  };

  /* 添加商品请求 */
  @action addGoods = () => {
    this.addForm.comspec = this.coms.map(item => item.val); // 数组
    this.addForm.gp = this.gps.map(item => item.val); // 数组

    this.addForm.goodsCw = this.addForm.goodsCw + this.addForm.goodsC;

    let form = new FormData();

    /*
    循环加入FormData 实现如下格式
        goodsName: 11
        goodsCode: 1111
        goodsCid: 2
        goodsJj: 111
        pllIds: 1,7
        goodsCothotr: 11
        goodsLq: 11
        goodsPrice: 11
        goodsCd: <p>123123</p>
        goodsCw: 11kg
        goodsC: kg
        goodsCi: 11
        goodsIw: 111
        goodsRs: 2
        goodsFss: 1
        goodsRemarks: 111
        file1: (binary)
        file2: (binary)
        file3: (binary)
        file3: (binary)
        comspec[0].comspecName: 3
        comspec[0].number: 3
        comspec[0].space: 3
        comspec[0].inventory: 3
        comspec[0].file: (binary)
        comspec[1].comspecName: 4
        comspec[1].number: 4
        comspec[1].space: 4
        comspec[1].inventory: 5
        comspec[1].file: (binary)
        gp[0].gpNogb: 1
        gp[0].gpPrice: 1
        gp[1].gpNogb: 2
        gp[1].gpPrice: 2
        goodsMp: 11
    */

    for (const key in this.addForm) {
      /* 是否为数组 */
      if (this.addForm[key] instanceof Array) {
        if (key !== 'pllIds' && key !== 'file3') {
          this.addForm[key].some((item, index) => {
            for (const k in item) {
              form.append(`${key}[${index}].${k}`, item[k]);
            }
          });
        } else if (key === 'file3') {
          this.addForm[key].some(item => {
            form.append(`${key}`, item);
          });
        } else {
          form.append(key, this.addForm[key]);
        }
      } else {
        /* 不是数组 */
        form.append(key, this.addForm[key]);
      }
    }
    return axios({
      url: '/mm-goods/goods/addGoods',
      method: 'post',
      data: form,
    }).then(({ data }) => {
      if (data.code == 200) {
        message.success('添加商品成功');
        return 200;
      }
    });
  };
  /* 修改商品请求 */
  @action modifyGoods = () => {
    this.addForm.comspec = this.coms.map(item => item.val); // 数组
    this.addForm.gp = this.gps.map(item => item.val); // 数组
    this.addForm.goodsCw = this.addForm.goodsCw + this.addForm.goodsC;
    let form = new FormData();
    for (const key in this.addForm) {
      if (this.addForm[key] != '') {
        /* 是否为数组 */
        if (this.addForm[key] instanceof Array) {
          if (key !== 'pllIds' && key !== 'file3') {
            this.addForm[key].some((item, index) => {
              for (const k in item) {
                form.append(`${key}[${index}].${k}`, item[k]);
              }
            });
          } else if (key === 'file3') {
            this.addForm[key].some(item => {
              form.append(`${key}`, item);
            });
          } else {
            form.append(key, this.addForm[key]);
          }
        } else {
          /* 不是数组 */
          form.append(key, this.addForm[key]);
        }
      }
    }
    // axios({
    //   url: '/goods/updateGoods',
    //   method: 'post',
    //   data: form,
    // }).then(({ data }) => {
    //   console.log(data);
    // });
  };
}
export default AddGoodsMobx;
